﻿using HDJ.Framework.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

namespace HDJ.Framework.Core
{
    public class GameSceneBase 
    {
        protected Dictionary<string, IWindowBase> UIDic = new Dictionary<string, IWindowBase>();

        public T GetUIWindow<T>() where T : IWindowBase
        {
            string name = typeof(T).Name;
            if (UIDic.ContainsKey(name))
                return (T)UIDic[name];
            return default(T);
        }

        public T OpenUIWindow<T>(int uiRootID = 0,params object[] param) where T:IWindowBase
        {
           return  (T)OpenUIWindow(typeof(T).Name, uiRootID, param);
        }

        public IWindowBase OpenUIWindow(string winName, int uiRootID = 0, params object[] param)
        {
            IWindowBase win= UIManager.OpenWindow(winName, uiRootID, param);
            UIDic.Add(winName, win);
            return win;
        }
        public void CloseNodeWindow(bool isImmediately = false)
        {
            IWindowBase win = UIManager.CloseNodeWindow(isImmediately);
            if (win!=null)
            {
                if (UIDic.ContainsKey(win.windowName))
                    UIDic.Remove(win.windowName);
                else
                    Debug.LogError("GameScene don't hava UI Window!");
            }
        }
        public void CloseUIWindow(IWindowBase win, bool isImmediately = false)
        {
            CloseUIWindow(win.windowName, isImmediately);
        }
        public void CloseUIWindow<T>(bool isImmediately = false)
        {
            CloseUIWindow(typeof(T).Name, isImmediately);
        }
        public void CloseUIWindow(string winName, bool isImmediately = false)
        {
            if (UIDic.ContainsKey(winName))
            {
                UIManager.CloseWindow(winName, isImmediately);
                UIDic.Remove(winName);
            }
            else
            {
                Debug.LogError("GameScene don't hava UI Window!");
            }
        }

        /// <summary>
        /// 初始化时调用，只调用一次
        /// </summary>
        public virtual void OnCreate() { }
        /// <summary>
        /// 场景打开时调用，每次打开都调用，在OnLoadAssets()前调用。
        /// </summary>
        public virtual void OnOpen() { }
        protected virtual void OnExit() { }
        public void OnExitEx()
        {
            foreach (var item in UIDic.Values)
            {
                UIManager.CloseWindow(item, true);
            }
          
            UIDic.Clear();
            OnExit();
        }
       
        public virtual void OnUpdate() { }

        public virtual void OnFixedUpdate() { }

        public virtual void OnLateUpdate() { }

        public virtual void OnGUIUpdate() { }
        public virtual void OnApplicationExit() { }

        /// <summary>
        /// 每次打开都调用
        /// </summary>
        /// <returns></returns>
        public virtual IEnumerator OnLoadAssets()
        {
            yield return null;
        }
  

    }

}